package src.view;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import com.jgoodies.forms.layout.*;
import org.jdesktop.swingx.*;
/*
 * Created by JFormDesigner on Sat May 29 10:08:22 ART 2010
 */


import src.controller.ManageTrustRelationshipsController;
import src.resource.PublicResource;




/**
 * @author Guillermo Exposito
 */
public class ManageTrustRelationships extends JPanel {
	
	/**
	 * 
	 */
	private final String PATH=PublicResource.getPath();
	private static final long serialVersionUID = 1L;
	private ManageTrustRelationshipsController controller;
	private static ManageTrustRelationships instance=null;
	
	public static ManageTrustRelationships getInstance(ManageTrustRelationshipsController controller){
		if(instance==null)
			instance=new ManageTrustRelationships(controller);
		return instance;
	}
	
	
	private ManageTrustRelationships(ManageTrustRelationshipsController controller) {
		this.controller=controller;
		initComponents();
		
		this.btnSearch.addActionListener(new ActionSearchPostOffices());
		this.jListPostOffice.addListSelectionListener(new ActionSelectItem());
		this.btnAdd.addActionListener(new ActionAddItem());
		this.btnRemove.addActionListener(new ActionRemoveItem());
		this.btnSave.addActionListener(new ActionUpdatePostOffice());
	}

	private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
		// Generated using JFormDesigner Evaluation license - Guillermo Exposito
		panel2 = new JPanel();
		lblTitle = new JLabel();
		panel1 = new JPanel();
		jPanelSearchPostOffce = new JPanel();
		lblSearchPostOffice = new JLabel();
		label1 = new JLabel();
		txtSearch = new JTextField();
		btnSearch = new JButton();
		scrollPane1 = new JScrollPane();
		jListPostOffice = new JList();
		btnSave = new JButton();
		jPanelTrustRelationships = new JPanel();
		label2 = new JLabel();
		scrollPane2 = new JScrollPane();
		jListAddedPostOffices = new JList();
		btnAdd = new JButton();
		btnRemove = new JButton();
		label3 = new JLabel();
		scrollPane3 = new JScrollPane();
		jListsAvailablePostOffices = new JList();
		CellConstraints cc = new CellConstraints();

		//======== this ========

		setLayout(new FormLayout(
			"left:default:grow",
			"fill:default, fill:default:grow"));

		//======== panel2 ========
		{
			panel2.setLayout(new FormLayout(
				"left:420dlu",
				"10dlu, fill:default, 5dlu"));

			//---- lblTitle ----
			lblTitle.setText("Manage Trust Relationships");
			lblTitle.setFont(new Font("Dialog", Font.PLAIN, 18));
			panel2.add(lblTitle, cc.xywh(1, 2, 1, 1, CellConstraints.CENTER, CellConstraints.DEFAULT));
		}
		add(panel2, cc.xy(1, 1));

		//======== panel1 ========
		{
			panel1.setLayout(new HorizontalLayout(1));

			//======== jPanelSearchPostOffce ========
			{
				jPanelSearchPostOffce.setLayout(new FormLayout(
					"20dlu, 22dlu, 3dlu, 100dlu, 3dlu, $lcgap, 62dlu, $lcgap, 5dlu",
					"2*(default, 10dlu), fill:default:grow, $lgap, default, 10dlu"));

				//---- lblSearchPostOffice ----
				lblSearchPostOffice.setText("Search Post Office");
				lblSearchPostOffice.setFont(new Font("Dialog", Font.PLAIN, 14));
				jPanelSearchPostOffce.add(lblSearchPostOffice, cc.xywh(2, 1, 3, 1));

				//---- label1 ----
				label1.setText("Name");
				jPanelSearchPostOffce.add(label1, cc.xywh(2, 3, 1, 1, CellConstraints.RIGHT, CellConstraints.DEFAULT));
				jPanelSearchPostOffce.add(txtSearch, cc.xy(4, 3));

				//---- btnSearch ----
				btnSearch.setText("Search");
				jPanelSearchPostOffce.add(btnSearch, cc.xywh(7, 3, 3, 1, CellConstraints.LEFT, CellConstraints.DEFAULT));

				//======== scrollPane1 ========
				{

					//---- jListPostOffice ----
					jListPostOffice.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
					scrollPane1.setViewportView(jListPostOffice);
				}
				jPanelSearchPostOffce.add(scrollPane1, cc.xywh(2, 5, 6, 1, CellConstraints.FILL, CellConstraints.FILL));

				//---- btnSave ----
				btnSave.setText("Save");
				jPanelSearchPostOffce.add(btnSave, cc.xywh(7, 7, 1, 1, CellConstraints.RIGHT, CellConstraints.DEFAULT));
			}
			panel1.add(jPanelSearchPostOffce);

			//======== jPanelTrustRelationships ========
			{
				jPanelTrustRelationships.setBorder(null);
				jPanelTrustRelationships.setLayout(new FormLayout(
					"10dlu, 96dlu, 97dlu, 5dlu, 10dlu",
					"33dlu, default, 3dlu, fill:default:grow, 2*($lgap, default), $lgap, default:grow, 10dlu"));

				//---- label2 ----
				label2.setText("Added Post Offices");
				label2.setFont(label2.getFont().deriveFont(label2.getFont().getSize() + 2f));
				jPanelTrustRelationships.add(label2, cc.xy(2, 2));

				//======== scrollPane2 ========
				{
					
					scrollPane2.setViewportView(jListAddedPostOffices);
				}
				jPanelTrustRelationships.add(scrollPane2, cc.xywh(2, 4, 2, 1, CellConstraints.DEFAULT, CellConstraints.FILL));

				//---- btnAdd ----
				btnAdd.setText("Add");
				jPanelTrustRelationships.add(btnAdd, cc.xywh(2, 6, 1, 1, CellConstraints.CENTER, CellConstraints.DEFAULT));

				//---- btnRemove ----
				btnRemove.setText("Remove");
				jPanelTrustRelationships.add(btnRemove, cc.xywh(3, 6, 1, 1, CellConstraints.CENTER, CellConstraints.DEFAULT));

				//---- label3 ----
				label3.setText("Available Post Offices");
				label3.setFont(label3.getFont().deriveFont(label3.getFont().getSize() + 2f));
				jPanelTrustRelationships.add(label3, cc.xy(2, 8));

				//======== scrollPane3 ========
				{
					
					scrollPane3.setViewportView(jListsAvailablePostOffices);
				}
				jPanelTrustRelationships.add(scrollPane3, cc.xywh(2, 10, 2, 1, CellConstraints.DEFAULT, CellConstraints.FILL));
			}
			panel1.add(jPanelTrustRelationships);
		}
		add(panel1, cc.xy(1, 2));
		// JFormDesigner - End of component initialization  //GEN-END:initComponents
		jListsAvailablePostOffices.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		jListAddedPostOffices.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		this.listModelAddedPostOffices=new DefaultListModel();
		this.listModelAvailablePostOffices=new DefaultListModel();
		this.listModelPostOffices=new DefaultListModel();
		lblTitle.setIcon(new ImageIcon(PATH+"application-pgp.png"));
		btnSave.setIcon(new ImageIcon(PATH+"document-save.png"));
		btnAdd.setIcon(new ImageIcon(PATH+"go-up.png"));
		btnRemove.setIcon(new ImageIcon(PATH+"go-down.png"));
		btnSearch.setIcon(new ImageIcon(PATH+"system-search.png"));
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
	// Generated using JFormDesigner Evaluation license - Guillermo Exposito
	private JPanel panel2;
	private JLabel lblTitle;
	private JPanel panel1;
	private JPanel jPanelSearchPostOffce;
	private JLabel lblSearchPostOffice;
	private JLabel label1;
	private JTextField txtSearch;
	private JButton btnSearch;
	private JScrollPane scrollPane1;
	private JList jListPostOffice;
	private JPanel jPanelTrustRelationships;
	private JLabel label2;
	private JScrollPane scrollPane2;
	private JList jListAddedPostOffices;
	private JButton btnAdd;
	private JButton btnRemove;
	private JLabel label3;
	private JScrollPane scrollPane3;
	private JList jListsAvailablePostOffices;
	private DefaultListModel listModelPostOffices,listModelAddedPostOffices,listModelAvailablePostOffices;
	private JButton btnSave;
	// JFormDesigner - End of variables declaration  //GEN-END:variables

	public JTextField getTxtSearch() {
		return txtSearch;
	}


	public void setTxtSearch(JTextField txtSearch) {
		this.txtSearch = txtSearch;
	}


	public JButton getBtnSearch() {
		return btnSearch;
	}


	public void setBtnSearch(JButton btnSearch) {
		this.btnSearch = btnSearch;
	}


	public JList getjListPostOffice() {
		return jListPostOffice;
	}


	public void setjListPostOffice(JList jListPostOffice) {
		this.jListPostOffice = jListPostOffice;
	}


	public JList getjListAddedPostOffices() {
		return jListAddedPostOffices;
	}


	public void setjListAddedPostOffices(JList jListAddedPostOffices) {
		this.jListAddedPostOffices = jListAddedPostOffices;
	}


	public JButton getBtnAdd() {
		return btnAdd;
	}


	public void setBtnAdd(JButton btnAdd) {
		this.btnAdd = btnAdd;
	}


	public JButton getBtnRemove() {
		return btnRemove;
	}


	public void setBtnRemove(JButton btnRemove) {
		this.btnRemove = btnRemove;
	}


	public JList getjListAvailablePostOffices() {
		return jListsAvailablePostOffices;
	}


	public void setjListsAvailablePostOffices(JList jListsAvailablePostOffices) {
		this.jListsAvailablePostOffices = jListsAvailablePostOffices;
	}


	public DefaultListModel getListModelPostOffices() {
		return listModelPostOffices;
	}


	public void setListModelPostOffices(DefaultListModel listModelPostOffices) {
		this.listModelPostOffices = listModelPostOffices;
	}


	public DefaultListModel getListModelAddedPostOffices() {
		return listModelAddedPostOffices;
	}


	public void setListModelAddedPostOffices(
			DefaultListModel listModelAddedPostOffices) {
		this.listModelAddedPostOffices = listModelAddedPostOffices;
	}


	public DefaultListModel getListModelAvailablePostOffices() {
		return listModelAvailablePostOffices;
	}


	public void setListModelAvailablePostOffices(
			DefaultListModel listModelAvailablePostOffices) {
		this.listModelAvailablePostOffices = listModelAvailablePostOffices;
	}
	
	public JButton getBtnSave() {
		return btnSave;
	}


	public void setBtnSave(JButton btnSave) {
		this.btnSave = btnSave;
	}

	private class ActionSearchPostOffices implements ActionListener {
		public void actionPerformed(ActionEvent e) {

			try {
				controller.performSearchPostOffices();
				//DialogResource.showSaveSucessfully(btnSave.getLocation());
				
			} catch (RemoteException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

		}
	}
	
	private class ActionSelectItem implements ListSelectionListener{

		@Override
		public void valueChanged(ListSelectionEvent arg0) {
			try {
				controller.performSelectItem();
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
	}
	
	private class ActionAddItem implements ActionListener {
		public void actionPerformed(ActionEvent e) {
				controller.performAdd();
		}
	}
	
	private class ActionRemoveItem implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			controller.performRemove();
		}
	}
	
	private class ActionUpdatePostOffice implements ActionListener {
		public void actionPerformed(ActionEvent e) {

			try {
				controller.performUpdatePostOffice();
				//DialogResource.showSaveSucessfully(btnSave.getLocation());
				
			} catch (RemoteException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

		}
	}
	
	
}